package tongcheng.com.tongcheng.ui.yemian.login.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yanzhenjie.nohttp.RequestMethod;

import org.json.JSONObject;

import butterknife.Bind;
import cn.jpush.android.api.JPushInterface;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.base.BaseActivity;
import tongcheng.com.tongcheng.bean.LoginSuccessBean;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.network.MyRequest;
import tongcheng.com.tongcheng.network.OnCallBack;
import tongcheng.com.tongcheng.ui.yemian.mapui.DaiLiQiangDanActivity;
import tongcheng.com.tongcheng.ui.yemian.mapui.GeRenFaDanActivity;
import tongcheng.com.tongcheng.ui.yemian.mapui.InstallQiangDanActivity;
import tongcheng.com.tongcheng.ui.yemian.mapui.OnePersonQiangDanActivity;
import tongcheng.com.tongcheng.ui.yemian.shenfen.activity.BaoZhenJinActivity;
import tongcheng.com.tongcheng.ui.yemian.shenfen.activity.InstallPeopleActivity;
import tongcheng.com.tongcheng.ui.yemian.shenfen.activity.ShenFenActivity;
import tongcheng.com.tongcheng.ui.yemian.shenfen.activity.WanShanZiLiaoActivity;
import tongcheng.com.tongcheng.ui.yemian.shenfen.activity.WanShanZiliao_QiyeActivity;
import tongcheng.com.tongcheng.ui.yemian.shenfen.activity.WanShanZiliao_Qiye_qiyeActivity;
import tongcheng.com.tongcheng.utils.GsonUtil;
import tongcheng.com.tongcheng.utils.Setting;
import tongcheng.com.tongcheng.utils.ShowToast;
import tongcheng.com.tongcheng.widght.MessageDialog;

public class LoginActivity extends BaseActivity {


    @Bind(R.id.login_icon)
    ImageView mLoginIcon;
    @Bind(R.id.login_phone)
    EditText mLoginPhone;
    @Bind(R.id.login_password)
    EditText mLoginPassword;
    @Bind(R.id.login_login)
    Button mLoginLogin;
    @Bind(R.id.login_wangjimima)
    TextView mLoginWangjimima;
    @Bind(R.id.login_zhuce)
    TextView mLoginZhuce;
    @Bind(R.id.test1)
    Button mTest1;
    @Bind(R.id.test2)
    EditText mTest2;
    private MessageDialog mMessageDialog;

    @Override
    public Integer initResid() {
        return R.layout.activity_login;
    }

    @Override
    public void initCreate(Bundle savedInstanceState) {

        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {

            finish();

            return;
        }

        mLoginPhone.setText(Setting.getPhone());

        mMessageDialog = new MessageDialog(this);
//        Eyes.translucentStatusBar(this, true);
        mTest1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              final  int i = Integer.parseInt(mTest2.getText().toString().trim());
                MyRequest request = new MyRequest(ConStans.TTTTT, RequestMethod.POST);
                request.add("phone", "13275218313");
                request.add("type",i);
                CallServer.getInstance().addPost(LoginActivity.this,request, 1, new OnCallBack() {
                    @Override
                    public void OnSuccess(int what, String json, Object ject) {
                        ShowToast.showToast(json);
                        Setting.setShenfen(i);
                    }
                });
            }
        });


        mLoginLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String password = mLoginPassword.getText().toString().trim();
                String phone = mLoginPhone.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ShowToast.showToast("手机号码不能为空");
                } else if (TextUtils.isEmpty(password)) {
                    ShowToast.showToast("密码不能为空");
                } else {
                    //startactivity(LoginActivity.this, DaoHangActivity.class);
                    login();
                }

                //startactivity(LoginActivity.this, ShenFenActivity.class);
                //startactivity(LoginActivity.this, StartShigongActivity.class);
            }
        });



        mLoginWangjimima.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              startactivity(LoginActivity.this, ForGetPassWordActivity.class);
               /* Intent intent = new Intent(LoginActivity.this, ZhuceActivity.class);
                startActivity(intent);*/
//                mMessageDialog.showit();
            }
        });



        mLoginZhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startactivity(LoginActivity.this, ZhuceActivity.class);
            }
        });
    }


    private void login() {
        MyRequest myRequest = new MyRequest(ConStans.Login_url, RequestMethod.POST);
        myRequest.add("phone", mLoginPhone.getText().toString().trim());
        myRequest.add("password", mLoginPassword.getText().toString().trim());
        myRequest.add("alias", JPushInterface.getRegistrationID(this));
        Log.e("个人设备id",JPushInterface.getRegistrationID(this) + "");
        CallServer.getInstance().addPost(this,myRequest, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object object) {
                Log.e("个人信息99",json + "");
                try {
                    JSONObject jsonObject = new JSONObject(json);
                    int status = jsonObject.getInt("status");
                    if (status == 0) {
                        String msg = jsonObject.getString("msg");
                        ShowToast.showToast(msg);
                    } else if (status == 1) {
                        Setting.clear();
                        LoginSuccessBean loginSuccessBean = GsonUtil.GsonToBean(json, LoginSuccessBean.class);
                        if (loginSuccessBean != null) {

                            Setting.setPhone(loginSuccessBean.data.phone);//存电话,唯一标识
                            Setting.setShenfen(loginSuccessBean.data.type);//存身份
                            Setting.setId(loginSuccessBean.data.id);//存id
                            Setting.setXinYongFen(loginSuccessBean.data.credit_score);//存信用分
                            Setting.setAddress(loginSuccessBean.data.address);//存地址
                            Setting.setAddress_detail(loginSuccessBean.data.address_detail);//存详细地址
                            Setting.setGuiMo(loginSuccessBean.data.scale);//存规模
                            Setting.setTouXiang(loginSuccessBean.data.avatar);//存头像
                            Setting.setUserName(loginSuccessBean.data.user_name);//存名字
                            Setting.setYuE(loginSuccessBean.data.balance);//存余额
                            Setting.setZhiFuBaoZhangHu(loginSuccessBean.data.ali_account);//存支付宝账户
                            Setting.setZhiFuBaoZhangHuName(loginSuccessBean.data.ali_real_name);//存支付宝账户姓名
                            if (loginSuccessBean.msg != null) {
                                ShowToast.showToast(loginSuccessBean.msg);
                            }
                            if (loginSuccessBean.data.state == 1) {//账号禁用

                            } else if(loginSuccessBean.data.state == 0){//正常
                                //去选择身份
                                if (loginSuccessBean.data.type == 0) {
                                    startactivity(LoginActivity.this, ShenFenActivity.class);
                                    finish();
                                }
                                //去完善信息
                                if (loginSuccessBean.data.is_write == 2 && loginSuccessBean.data.type != 0) {
                                    switch (loginSuccessBean.data.type) {
                                        case 1://企业发单
                                            startactivity(LoginActivity.this, WanShanZiliao_Qiye_qiyeActivity.class);
                                            break;
                                        case 2://个人发单
                                            startactivity(LoginActivity.this, WanShanZiLiaoActivity.class);
                                            break;
                                        case 3://代理商抢单
                                            startactivity(LoginActivity.this, WanShanZiliao_QiyeActivity.class);
                                            break;
                                        case 4://个人抢单
                                            startactivity(LoginActivity.this, InstallPeopleActivity.class);
                                            break;
                                        case 5://安装人员
                                            startactivity(LoginActivity.this, InstallPeopleActivity.class);
                                            break;
                                    }
                                    finish();
                                }
                                //去交钱
                                if (loginSuccessBean.data.is_pledge == 2 && loginSuccessBean.data.type != 0) {
                                    startactivity(LoginActivity.this, BaoZhenJinActivity.class);
                                    finish();
                                }
                                //根据身份进入不同的地图页面
                                if (loginSuccessBean.data.type != 0 && loginSuccessBean.data.is_write == 1
                                        && loginSuccessBean.data.is_pledge == 1) {
                                    switch (loginSuccessBean.data.type) {
                                        case 1://企业发单
                                            startactivity(LoginActivity.this, GeRenFaDanActivity.class);
                                            break;
                                        case 2://个人发单
                                            startactivity(LoginActivity.this, GeRenFaDanActivity.class);
                                            break;
                                        case 3://代理商抢单
                                            startactivity(LoginActivity.this, DaiLiQiangDanActivity.class);
                                            break;
                                        case 4://个人抢单
                                            startactivity(LoginActivity.this, OnePersonQiangDanActivity.class);
                                            break;
                                        case 5://安装人员
                                            startactivity(LoginActivity.this, InstallQiangDanActivity.class);
                                            break;
                                    }
                                    finish();
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    Log.e("login", "解析失败");
                }


            }
        });
    }



}
